package viettravel.guiandevent.map;

import org.holoeverywhere.app.ProgressDialog;
import org.json.JSONException;
import org.json.JSONObject;

import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.map.search.GGDataReader;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class GetDirectionDialog extends Activity {
	EditText editFrom;
	EditText editTo;
	CheckBox cbxMyLocation;
	String mylocation;
	String place;
	String placeName;
	ImageButton btndriving;
	ImageButton btnwaling;
	String startLocation;
	String endLocation;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get_direction_dialog);
		setTitle(getString(R.string.getdirectiondialog_title));
		editFrom=(EditText) findViewById(R.id.editFrom);
		editFrom.setTypeface(Constants.TYPEFACE_NYALA);
		editFrom.setTextSize(24);
		
		editTo=(EditText) findViewById(R.id.editTo);
		editTo.setTypeface(Constants.TYPEFACE_NYALA);
		editTo.setTextSize(24);
		
		TextView getDirectionFrom = (TextView)findViewById(R.id.getDirectionFrom);
		TextView getDirectionTo = (TextView)findViewById(R.id.getDirectionTo);
		TextView getDirectionUseMyLocation = (TextView)findViewById(R.id.getDirectionUseMyLocation);
		
		getDirectionFrom.setTypeface(Constants.TYPEFACE_NYALA);
		getDirectionFrom.setTextSize(24);
		
		getDirectionTo.setTypeface(Constants.TYPEFACE_NYALA);
		getDirectionTo.setTextSize(24);
		
		getDirectionUseMyLocation.setTypeface(Constants.TYPEFACE_NYALA);
		getDirectionUseMyLocation.setTextSize(24);
		
		cbxMyLocation=(CheckBox) findViewById(R.id.checkBoxMyLocation);
		btndriving=(ImageButton) findViewById(R.id.btnDrivingMode);
		btndriving.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_moto));
		btnwaling=(ImageButton) findViewById(R.id.btnWalkingMode);
		btnwaling.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_walking));
		Bundle extra=getIntent().getExtras();
		if(extra!=null){
			mylocation=extra.getString(Constants.MY_LOCATION);
			place=extra.getString(Constants.PLACE_WANT_SEARCH);
			placeName=extra.getString(Constants.TAG_PLACES_NAME);
			if(place!=null||placeName!=null){
				endLocation=place;
				Log.d(Constants.TAG,place);
				editTo.setText(placeName);
				editTo.setEnabled(false);
			}
		}
		btndriving.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(cbxMyLocation.isChecked()){
					startLocation=mylocation;
				}
				else{
					startLocation=editFrom.getText().toString();
				}
				if ((editFrom.getText().toString().equals("")) || (editTo.getText().toString().equals(""))) {
					return;
				}
				else{
					StringBuilder urlBuilder =new StringBuilder();
					urlBuilder.append("http://maps.googleapis.com/maps/api/directions/json?origin=");
					startLocation= Constants.convertUTF8String(startLocation.replace(" ", "%20"));
					urlBuilder.append(startLocation);
					urlBuilder.append("&destination=");
					if(editTo.isEnabled()){
						endLocation=editTo.getText().toString();
						endLocation=Constants.convertUTF8String(endLocation.replace(" ", "%20"));
					}
					urlBuilder.append(endLocation);
					urlBuilder.append("&sensor=false&units=metric&language=vi");
					urlBuilder.append("&mode=");
					urlBuilder.append("driving");
					Log.d("URL", urlBuilder.toString());
					new Direction().execute(urlBuilder.toString());
				}
				
			}
		});
		
		btnwaling.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(cbxMyLocation.isChecked()){
					startLocation=mylocation;
				}
				else{
					startLocation=editFrom.getText().toString();
				}
				if ((editFrom.getText().toString().equals("")) || (editTo.getText().toString().equals(""))) {
					return;
				}
				else{
					StringBuilder urlBuilder =new StringBuilder();
					urlBuilder.append("http://maps.googleapis.com/maps/api/directions/json?origin=");
					startLocation= Constants.convertUTF8String(startLocation.replace(" ", "%20"));
					urlBuilder.append(startLocation);
					urlBuilder.append("&destination=");
					if(editTo.isEnabled()){
						endLocation=editTo.getText().toString();
						endLocation=Constants.convertUTF8String(endLocation.replace(" ", "%20"));
					}
					urlBuilder.append(endLocation);
					urlBuilder.append("&sensor=false&units=metric&language=vi");
					urlBuilder.append("&mode=");
					urlBuilder.append("walking");
					new Direction().execute(urlBuilder.toString());
				}
				
			}
		});
		cbxMyLocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked==true){
					editFrom.setText("My Current Location");
					editFrom.setEnabled(!isChecked);
					startLocation=mylocation;
				}
				else{
					editFrom.setText("");
					editFrom.setEnabled(!isChecked);
					editFrom.setFocusable(!isChecked);
					
				}
					
				
			}
		});
		cbxMyLocation.setChecked(true);
		
	}
	public class Direction extends AsyncTask<String, Integer, String>{
		
		private ProgressDialog mProgressDialog;

		@Override
		protected void onPreExecute() {
			mProgressDialog = new ProgressDialog(GetDirectionDialog.this);
			mProgressDialog.setMessage("Get direction");
			mProgressDialog.setCancelable(false);
			mProgressDialog.setIndeterminate(true);
			mProgressDialog.show();
		};

		@Override
		protected String doInBackground(String... params) {
			JSONObject jsonObject = GGDataReader.getJsonFromUrl(params[0]);
			try {
				if (jsonObject.getString(Constants.TAG_STATUS).equals("OK")) {
					return jsonObject.toString();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "FAIL";
		}
		@Override
		protected void onPostExecute(String result)
		{
			if (mProgressDialog.isShowing()) 
			{
				mProgressDialog.dismiss();
				if(!result.equals("FAIL")){
					Intent intent=new Intent(getBaseContext(),DirectionListActivity.class);
					intent.putExtra(Constants.DIRECTIONLIST, result);
					intent.putExtra(Constants.TAG_PLACES_NAME, placeName);
					Log.d("GET DIRECTION", result);
					startActivity(intent);
					finish();
				}
			}
		}		
	}
	
}
